﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceApplication.Service.Basic;
using CommonLibrary.Common;
using OAclient.Models;
using OAclient.Filters;

namespace OAclient.Controllers
{
    [VaildateLogin]
    [ExceptionFilter]
    [LoggerFilter]
    public class AuthorityController : Controller
    {
        //
        // GET: /Authority/

        BasicService bs = new BasicService();
        public ActionResult Auth()
        {
            var user = bs.QueryAllUsers();
            var auth = bs.QueryAllAuth();
            ViewData["employee"] = produEmployee(user);
            ViewData["carrers"] = produCarre(auth);


            ViewData["menuzTree"] = ZTreeHelper.Serialize(bs.GetMenuNodeList());
            return View();
        }


        #region 方法

        public ActionResult changeAuth(String userId, String authId)
        {
            if (!String.IsNullOrEmpty(userId) && !String.IsNullOrEmpty(authId))
                bs.ChangeAuth(userId, authId);
            return RedirectToAction("GetUser", new { id = userId });
        }

        public ActionResult AddAuth(String authName, String authority)
        {
            if (String.IsNullOrEmpty(authName))
                return JavaScript("alert('类型名称不能为空')");

            bs.AddAuth(authName, authority);

            return RedirectToAction("Auth");
        }

        public ActionResult GetUser(String id)
        {
            if (String.IsNullOrEmpty(id)) return Content("");
            var auth = bs.QueryUserById(id);
            return Content(auth);
        }

        public ActionResult RefreshPerszTree(String authId)
        {
            var zTree = ZTreeHelper.Serialize(bs.GetPersMenuNodes(authId));
            return Content(zTree);
        }

        public ActionResult AuthChange(String authId, String authority)
        {
            if (string.IsNullOrEmpty(authId) || string.IsNullOrEmpty(authority))
                return Json(new ResTag(false));

            bs.AuthChange(authId, authority);
            return Json(new ResTag(true));
        }

        public ActionResult getMenuTree(bool? nocheck)
        {
            if (null == nocheck)
            {
                nocheck = false;//默认是有check的树
            }
            //var authority = bs.AuthQueryAutority(authId);
            //var list_userauth = CommonMethod.splitString(authority, ';').ToList();

            var dt_menu = bs.MenuQueryAll();
            var list_rootmenu = dt_menu.Select("menu_supid = '0' ").ToList();
            var list_chlidmenu = dt_menu.Select().ToList();
            var childtree = from q in list_rootmenu
                            select new
                            {
                                id = q["menu_id"].ToString(),
                                name = q["menu_name"].ToString(),
                                nocheck = nocheck,
                                open = false,
                                tag = "",
                                children = from p in list_chlidmenu
                                           where p["menu_supid"].ToString() == q["menu_id"].ToString()
                                           select new { id = p["menu_id"].ToString(), name = p["menu_name"].ToString(), nocheck = nocheck, open = false, tag = "" }
                            };
            var objtree = new { id = -1, name = "功能列表", nocheck = nocheck, open = true, children = childtree };
            return Json(objtree);
        }

        #region 私有方法

        private List<SelectListItem> produEmployee(System.Data.DataTable dt)
        {
            List<SelectListItem> select = new List<SelectListItem>();
            Int32 rowCount = dt.Rows.Count;
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                var text = dt.Rows[rowIndex]["用户名称"].ToString();
                var value = dt.Rows[rowIndex]["用户编码"].ToString();
                select.Add(new SelectListItem
                {
                    Text = text,
                    Value = value
                });
            }
            return select;
        }

        private List<SelectListItem> produCarre(System.Data.DataTable dt)
        {
            List<SelectListItem> select = new List<SelectListItem>();

            for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
            {
                select.Add(new SelectListItem
                {
                    Text = dt.Rows[rowIndex][1].ToString(),
                    Value = dt.Rows[rowIndex][0].ToString()
                });

            }
            return select;
        }

        #endregion

        #endregion
    }
}
